﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using N03_11HCA_HTTTHD.Models;

using Data_DAO_DTO_Lib.DTO;

namespace N03_11HCA_HTTTHD.Controllers
{
    public class TaiKhoanController : Controller
    {
        Data_DAO_DTO_Lib.TAIKHOAN_DAO tkDAO = new Data_DAO_DTO_Lib.TAIKHOAN_DAO();
        Data_DAO_DTO_Lib.DANHMUC_DAO danhMucDAO = new Data_DAO_DTO_Lib.DANHMUC_DAO();
        Data_DAO_DTO_Lib.LOAITAIKHOAN_DAO ltkDAO = new Data_DAO_DTO_Lib.LOAITAIKHOAN_DAO();
        Data_DAO_DTO_Lib.LOAITHANHVIEN_DAO ltvDAO = new Data_DAO_DTO_Lib.LOAITHANHVIEN_DAO();
        public ActionResult DangKy()
        {

            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            List<Data_DAO_DTO_Lib.DTO.DTO> lLoaiTV = ltvDAO.LayDanhSach();
            List < Data_DAO_DTO_Lib.DTO.DTO > lLoaiTK = ltkDAO.LayDanhSach();
            List<Data_DAO_DTO_Lib.DTO.DTO> listPhanLoai = danhMucDAO.LayDanhSach();
            N03_11HCA_HTTTHD.Models.TaiKhoan_DangKy model = new TaiKhoan_DangKy(null, "", lLoaiTK, lLoaiTV, listPhanLoai);
            return View("TaiKhoan_DangKy", model);
        }

        //
        // POST: /TaiKhoan/DangKy
        [HttpPost]
        public ActionResult DangKy(FormCollection collection)
        {  
            //System.Diagnostics.Debug.WriteLine(collection.Count.ToString());
            //return RedirectToAction("Index");
            TAIKHOAN_DTO tk = new TAIKHOAN_DTO();
            try
            {

                tk.DiaChi = collection[TaiKhoan_DangKy.Name_diaChi];
                tk.DiDong = collection[TaiKhoan_DangKy.Name_diDong];
                tk.DienThoai = collection[TaiKhoan_DangKy.Name_dienThoai];
                tk.Email = collection[TaiKhoan_DangKy.Name_email];
                tk.Fax = collection[TaiKhoan_DangKy.Name_fax];
                tk.GioiTinh = collection[TaiKhoan_DangKy.Name_gioiTinh];
                tk.HoTen = collection[TaiKhoan_DangKy.Name_hoTen];
                tk.MatKhau = collection[TaiKhoan_DangKy.Name_matKhau];
                //tk.NgaySinh = collection[TaiKhoan_DangKy.Name_ngaySinh];
                tk.NgaySinh = DateTime.Now;
                tk.TenDangNhap = collection[TaiKhoan_DangKy.Name_tenDangNhap];

                tk.IDLoaiTK = Int32.Parse(collection[TaiKhoan_DangKy.Name_loaiTaiKhoan]);
                tk.IDLoaiTV = Int32.Parse(collection[TaiKhoan_DangKy.Name_loaiThanhVien]);
                String[] danhMucQuanTam = collection.GetValues(TaiKhoan_DangKy.Name_linhVucQuanTam);
                List<int> ldanhMucQuanTam = new List<int>();
                for (int i = 0; i < danhMucQuanTam.Length; i++)
                {
                    int idDM = int.Parse(danhMucQuanTam[i]);

                    ldanhMucQuanTam.Add(idDM);
                }
                if (tk.IDLoaiTV == 2)
                {

                    DOANHNGHIEP_DTO dn = new DOANHNGHIEP_DTO();
                    dn.CacSanPhamDaSanXuat = collection[TaiKhoan_DangKy.Name_sanPhamChuyenGiaoDoanhNghiep];
                    dn.ChucNangCongTy = collection[TaiKhoan_DangKy.Name_chucNangDoanhNghiep];
                    dn.DiaChi = collection[TaiKhoan_DangKy.Name_diaChiDoanhNghiep];
                    dn.GioiThieu = collection[TaiKhoan_DangKy.Name_gioiThieuDoanhNghiep];
                    dn.HinhAnh = collection[TaiKhoan_DangKy.Name_hinhAnhDoanhNghiep];
                    String ngayTL = collection[TaiKhoan_DangKy.Name_ngayThanhLapDoanhNghiep];
                    dn.NgayThanhLap = DateTime.Parse(ngayTL);

                    dn.NguoiDaiDien = collection[TaiKhoan_DangKy.Name_nguoiDaiDienDoanhNghiep];
                    dn.TenCuaHang = collection[TaiKhoan_DangKy.Name_tenDoanhNghiep];
                    dn.TongSoNhanVien = int.Parse(collection[TaiKhoan_DangKy.Name_tongSoNhanVienDoanhNghiep]);

                    String[] linhVucQuanTamDoanhNghiep = collection.GetValues(TaiKhoan_DangKy.Name_linhVucQuanTamDoanhNghiep);
                    List<int> ldanhMucQuanTamDN = new List<int>();
                    for (int i = 0; i < linhVucQuanTamDoanhNghiep.Length; i++)
                    {
                        int idDM = int.Parse(linhVucQuanTamDoanhNghiep[i]);

                        ldanhMucQuanTamDN.Add(idDM);
                    }
                    tkDAO.Them(tk, ldanhMucQuanTam, dn, ldanhMucQuanTamDN);
                }
                else
                {
                    //Thêm
                    tkDAO.Them(tk,ldanhMucQuanTam);
                }
            }
            catch (Exception e)
            {

                List<Data_DAO_DTO_Lib.DTO.DTO> lLoaiTV = ltvDAO.LayDanhSach();
                List<Data_DAO_DTO_Lib.DTO.DTO> lLoaiTK = ltkDAO.LayDanhSach();
                List<Data_DAO_DTO_Lib.DTO.DTO> listPhanLoai = danhMucDAO.LayDanhSach();
                N03_11HCA_HTTTHD.Models.TaiKhoan_DangKy modelDK = new TaiKhoan_DangKy(tk, e.Message, lLoaiTK, lLoaiTV, listPhanLoai);
                return View("TaiKhoan_DangKy", modelDK);
            }

            N03_11HCA_HTTTHD.Models.ThongBao model = new ThongBao("Đăng ký tài khoản thành công, hãy đăng nhập và tham gia vào Chợ Điện Tử, Công Nghệ.");
            return View("ThongBao", model);
        }

        Data_DAO_DTO_Lib.DOANHNGHIEP_DAO dnDAO = new Data_DAO_DTO_Lib.DOANHNGHIEP_DAO();

        public ActionResult ThongTinCaNhan()
        {
            
            ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
            if (TK.ListQuyen == null)
            {
                return RedirectToAction("Index", "Home");
            }

            ViewBag.TK = TK;

            List<Data_DAO_DTO_Lib.DTO.DTO> listPhanLoai = danhMucDAO.LayDanhSach();

            TAIKHOAN_DTO tkhoan = (TAIKHOAN_DTO)tkDAO.LayTuID((int)TK.ID1);
            DTO ltv =  ltvDAO.LayTuID( tkhoan.IDLoaiTV);
            DTO ltk = ltkDAO.LayTuID(tkhoan.IDLoaiTK);

            DOANHNGHIEP_DTO dn = null;
             List<Data_DAO_DTO_Lib.DTO.DTO> lDMQTDN = null;
            if (tkhoan.IDDoanhNghiep!= null)
	        {
                dn = (DOANHNGHIEP_DTO)dnDAO.LayTuID((int)tkhoan.IDDoanhNghiep);
                lDMQTDN = danhMucDAO.LayDanhSachDanhMucQuanTamDN((int)tkhoan.IDDoanhNghiep);
	        }

            List<Data_DAO_DTO_Lib.DTO.DTO> lDMQT = danhMucDAO.LayDanhSachDanhMucQuanTamTaiKhoan((int)TK.ID1);
            

            N03_11HCA_HTTTHD.Models.TaiKhoan_ThongTinCaNhan model = new TaiKhoan_ThongTinCaNhan(tkhoan, "", ltk, ltv, listPhanLoai, dn, lDMQT, lDMQTDN);
            return View("TaiKhoan_ThongTinCaNhan", model);
        }


        
        //
        // POST: /TaiKhoan/DangKy
        [HttpPost]
        public ActionResult ThongTinCaNhan(FormCollection collection)
        {
            ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
            if (TK.ListQuyen == null)
            {
                return RedirectToAction("Index", "Home");
            }

            ViewBag.TK = TK;

            TAIKHOAN_DTO tk = new TAIKHOAN_DTO();
            tk.ID = (int)TK.ID1;
            try
            {
                tk.DiaChi = collection[TaiKhoan_DangKy.Name_diaChi];
                tk.DiDong = collection[TaiKhoan_DangKy.Name_diDong];
                tk.DienThoai = collection[TaiKhoan_DangKy.Name_dienThoai];
                tk.Email = collection[TaiKhoan_DangKy.Name_email];
                tk.Fax = collection[TaiKhoan_DangKy.Name_fax];
                tk.GioiTinh = collection[TaiKhoan_DangKy.Name_gioiTinh];
                tk.HoTen = collection[TaiKhoan_DangKy.Name_hoTen];
                tk.MatKhau = collection[TaiKhoan_DangKy.Name_matKhau];
                //tk.NgaySinh = collection[TaiKhoan_DangKy.Name_ngaySinh];
                tk.NgaySinh = DateTime.Now;
                //tk.TenDangNhap = collection[TaiKhoan_DangKy.Name_tenDangNhap];

                //tk.IDLoaiTK = Int32.Parse(collection[TaiKhoan_DangKy.Name_loaiTaiKhoan]);
                //tk.IDLoaiTV = Int32.Parse(collection[TaiKhoan_DangKy.Name_loaiThanhVien]);
                String[] danhMucQuanTam = collection.GetValues(TaiKhoan_DangKy.Name_linhVucQuanTam);
                List<int> ldanhMucQuanTam = new List<int>();
                for (int i = 0; i < danhMucQuanTam.Length; i++)
                {
                    int idDM = int.Parse(danhMucQuanTam[i]);

                    ldanhMucQuanTam.Add(idDM);
                }

                //Cập nhật
                tkDAO.CapNhat(tk, ldanhMucQuanTam);
            }
            catch (Exception e)
            {

                List<Data_DAO_DTO_Lib.DTO.DTO> lLoaiTV = ltvDAO.LayDanhSach();
                List<Data_DAO_DTO_Lib.DTO.DTO> lLoaiTK = ltkDAO.LayDanhSach();
                List<Data_DAO_DTO_Lib.DTO.DTO> listPhanLoai = danhMucDAO.LayDanhSach();

                TAIKHOAN_DTO tkhoan = (TAIKHOAN_DTO)tkDAO.LayTuID((int)TK.ID1);
                LOAITHANHVIEN_DTO ltv = (LOAITHANHVIEN_DTO)ltvDAO.LayTuID(tkhoan.IDLoaiTV);
                LOAITAIKHOAN_DTO ltk = (LOAITAIKHOAN_DTO)ltkDAO.LayTuID(tkhoan.IDLoaiTK);

                DOANHNGHIEP_DTO dn = null;
                List<Data_DAO_DTO_Lib.DTO.DTO> lDMQTDN = null;
                if (tkhoan.IDDoanhNghiep != null)
                {
                    dn = (DOANHNGHIEP_DTO)dnDAO.LayTuID((int)tkhoan.IDDoanhNghiep);
                    lDMQTDN = danhMucDAO.LayDanhSachDanhMucQuanTamDN((int)tkhoan.IDDoanhNghiep);
                }

                List<Data_DAO_DTO_Lib.DTO.DTO> lDMQT = danhMucDAO.LayDanhSachDanhMucQuanTamTaiKhoan((int)TK.ID1);


                N03_11HCA_HTTTHD.Models.TaiKhoan_ThongTinCaNhan model = new TaiKhoan_ThongTinCaNhan(tkhoan, e.Message, ltk, ltv, listPhanLoai, dn, lDMQT, lDMQTDN);
                return View("TaiKhoan_ThongTinCaNhan", model);
            }

            return RedirectToAction("ThongTinCaNhan", "TaiKhoan");
        }

        public ActionResult ThongTinDoanhNghiep()
        {
            return ThongTinCaNhan();
        }


        [HttpPost]
        public ActionResult ThongTinDoanhNghiep(FormCollection collection)
        {
            ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
            if (TK.ListQuyen == null)
            {
                return RedirectToAction("Index", "Home");
            }

            ViewBag.TK = TK;
            TAIKHOAN_DTO tk = new TAIKHOAN_DTO();
            try
            {
                tk.ID = (int)TK.ID1;
                tk.DiaChi = collection[TaiKhoan_DangKy.Name_diaChi];
                tk.DiDong = collection[TaiKhoan_DangKy.Name_diDong];
                tk.DienThoai = collection[TaiKhoan_DangKy.Name_dienThoai];
                tk.Email = collection[TaiKhoan_DangKy.Name_email];
                tk.Fax = collection[TaiKhoan_DangKy.Name_fax];
                tk.GioiTinh = collection[TaiKhoan_DangKy.Name_gioiTinh];
                tk.HoTen = collection[TaiKhoan_DangKy.Name_hoTen];
                tk.MatKhau = collection[TaiKhoan_DangKy.Name_matKhau];
                //tk.NgaySinh = collection[TaiKhoan_DangKy.Name_ngaySinh];
                tk.NgaySinh = DateTime.Now;
                tk.TenDangNhap = collection[TaiKhoan_DangKy.Name_tenDangNhap];

                //tk.IDLoaiTK = Int32.Parse(collection[TaiKhoan_DangKy.Name_loaiTaiKhoan]);
                //tk.IDLoaiTV = Int32.Parse(collection[TaiKhoan_DangKy.Name_loaiThanhVien]);
                String[] danhMucQuanTam = collection.GetValues(TaiKhoan_DangKy.Name_linhVucQuanTam);
                List<int> ldanhMucQuanTam = new List<int>();
                for (int i = 0; i < danhMucQuanTam.Length; i++)
                {
                    int idDM = int.Parse(danhMucQuanTam[i]);

                    ldanhMucQuanTam.Add(idDM);
                }

                    DOANHNGHIEP_DTO dn = new DOANHNGHIEP_DTO();
                    dn.CacSanPhamDaSanXuat = collection[TaiKhoan_DangKy.Name_sanPhamChuyenGiaoDoanhNghiep];
                    dn.ChucNangCongTy = collection[TaiKhoan_DangKy.Name_chucNangDoanhNghiep];
                    dn.DiaChi = collection[TaiKhoan_DangKy.Name_diaChiDoanhNghiep];
                    dn.GioiThieu = collection[TaiKhoan_DangKy.Name_gioiThieuDoanhNghiep];
                    dn.HinhAnh = collection[TaiKhoan_DangKy.Name_hinhAnhDoanhNghiep];
                    String ngayTL = collection[TaiKhoan_DangKy.Name_ngayThanhLapDoanhNghiep];
                    dn.NgayThanhLap = DateTime.Parse(ngayTL);

                    dn.NguoiDaiDien = collection[TaiKhoan_DangKy.Name_nguoiDaiDienDoanhNghiep];
                    dn.TenCuaHang = collection[TaiKhoan_DangKy.Name_tenDoanhNghiep];
                    dn.TongSoNhanVien = int.Parse(collection[TaiKhoan_DangKy.Name_tongSoNhanVienDoanhNghiep]);

                    String[] linhVucQuanTamDoanhNghiep = collection.GetValues(TaiKhoan_DangKy.Name_linhVucQuanTamDoanhNghiep);
                    List<int> ldanhMucQuanTamDN = new List<int>();
                    for (int i = 0; i < linhVucQuanTamDoanhNghiep.Length; i++)
                    {
                        int idDM = int.Parse(linhVucQuanTamDoanhNghiep[i]);

                        ldanhMucQuanTamDN.Add(idDM);
                    }
                    tkDAO.CapNhat(tk, ldanhMucQuanTam, dn, ldanhMucQuanTamDN);
            }
            catch (Exception e)
            {

                List<Data_DAO_DTO_Lib.DTO.DTO> lLoaiTV = ltvDAO.LayDanhSach();
                List<Data_DAO_DTO_Lib.DTO.DTO> lLoaiTK = ltkDAO.LayDanhSach();
                List<Data_DAO_DTO_Lib.DTO.DTO> listPhanLoai = danhMucDAO.LayDanhSach();

                TAIKHOAN_DTO tkhoan = (TAIKHOAN_DTO)tkDAO.LayTuID((int)TK.ID1);
                LOAITHANHVIEN_DTO ltv = (LOAITHANHVIEN_DTO)ltvDAO.LayTuID(tkhoan.IDLoaiTV);
                LOAITAIKHOAN_DTO ltk = (LOAITAIKHOAN_DTO)ltkDAO.LayTuID(tkhoan.IDLoaiTK);

                DOANHNGHIEP_DTO dn = null;
                List<Data_DAO_DTO_Lib.DTO.DTO> lDMQTDN = null;
                if (tkhoan.IDDoanhNghiep != null)
                {
                    dn = (DOANHNGHIEP_DTO)dnDAO.LayTuID((int)tkhoan.IDDoanhNghiep);
                    lDMQTDN = danhMucDAO.LayDanhSachDanhMucQuanTamDN((int)tkhoan.IDDoanhNghiep);
                }

                List<Data_DAO_DTO_Lib.DTO.DTO> lDMQT = danhMucDAO.LayDanhSachDanhMucQuanTamTaiKhoan((int)TK.ID1);


                N03_11HCA_HTTTHD.Models.TaiKhoan_ThongTinCaNhan model = new TaiKhoan_ThongTinCaNhan(tkhoan, e.Message, ltk, ltv, listPhanLoai, dn, lDMQT, lDMQTDN);
                return View("TaiKhoan_ThongTinCaNhan", model);
            }

            return RedirectToAction("ThongTinCaNhan", "TaiKhoan");

        }


        public ActionResult DoiMatKhau()
        {
            return ThongTinCaNhan();
        }

        //
        // POST: /TaiKhoan/DangKy
        [HttpPost]
        public ActionResult DoiMatKhau(FormCollection collection)
        {
            ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
            if (TK.ListQuyen == null)
            {
                return RedirectToAction("Index", "Home");
            }

            ViewBag.TK = TK;

            
            int IDTK = (int)TK.ID1;

            try
            {
                String mkCu = collection[TaiKhoan_DangKy.Name_matKhau_cu];
                String mkMoi = collection[TaiKhoan_DangKy.Name_matKhau];
                
                //Cập nhật
                tkDAO.DoiMatKhau(IDTK,mkCu,mkMoi);
            }
            catch (Exception e)
            {

                List<Data_DAO_DTO_Lib.DTO.DTO> lLoaiTV = ltvDAO.LayDanhSach();
                List<Data_DAO_DTO_Lib.DTO.DTO> lLoaiTK = ltkDAO.LayDanhSach();
                List<Data_DAO_DTO_Lib.DTO.DTO> listPhanLoai = danhMucDAO.LayDanhSach();

                TAIKHOAN_DTO tkhoan = (TAIKHOAN_DTO)tkDAO.LayTuID((int)TK.ID1);
                LOAITHANHVIEN_DTO ltv = (LOAITHANHVIEN_DTO)ltvDAO.LayTuID(tkhoan.IDLoaiTV);
                LOAITAIKHOAN_DTO ltk = (LOAITAIKHOAN_DTO)ltkDAO.LayTuID(tkhoan.IDLoaiTK);

                DOANHNGHIEP_DTO dn = null;
                List<Data_DAO_DTO_Lib.DTO.DTO> lDMQTDN = null;
                if (tkhoan.IDDoanhNghiep != null)
                {
                    dn = (DOANHNGHIEP_DTO)dnDAO.LayTuID((int)tkhoan.IDDoanhNghiep);
                    lDMQTDN = danhMucDAO.LayDanhSachDanhMucQuanTamDN((int)tkhoan.IDDoanhNghiep);
                }

                List<Data_DAO_DTO_Lib.DTO.DTO> lDMQT = danhMucDAO.LayDanhSachDanhMucQuanTamTaiKhoan((int)TK.ID1);


                N03_11HCA_HTTTHD.Models.TaiKhoan_ThongTinCaNhan model = new TaiKhoan_ThongTinCaNhan(tkhoan, e.Message, ltk, ltv, listPhanLoai, dn, lDMQT, lDMQTDN);
                return View("TaiKhoan_ThongTinCaNhan", model);
            }

            return RedirectToAction("ThongTinCaNhan", "TaiKhoan");
        }
    }

}
